package com.ruoyi.energize.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.energize.util.GetGenderData;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.energize.domain.WdzDepartments;
import com.ruoyi.energize.service.IWdzDepartmentsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 万店掌门店列表Controller
 *
 * @author ruoyi
 * @date 2023-06-14
 */
@RestController
@RequestMapping("/energize/departments")
public class WdzDepartmentsController extends BaseController
{
    @Autowired
    private IWdzDepartmentsService wdzDepartmentsService;


    /**
     * 查询万店掌门店列表数据同步到门店列表中
     */
    //@GetMapping("/moveDepartmentsList")
    public void moveDepartmentsList(){
        GetGenderData getGenderData=new GetGenderData();
        String departmentsString=getGenderData.getDepartments();
        this.stringToJsonobject(departmentsString);
    }

    public void stringToJsonobject(String stringJson){
        JSONObject jsonObject = (JSONObject) JSONObject.parse(stringJson);
        System.out.println(jsonObject.toString());
        //获取 msg
        System.out.println(jsonObject.get("result"));
        if(jsonObject.get("result").equals("SUCCESS")){
            JSONObject data= JSONObject.from(jsonObject.get("data"));
            JSONArray jsonArrayData = data.getJSONArray("rows");
            List<WdzDepartments> wdzDepartments = jsonArrayData.toJavaList(WdzDepartments.class);
            if (wdzDepartments.size()>0) {//证明存在门店
                //使用list批量插入到万店掌门店列表中去
                for (WdzDepartments wdzDepartments1:wdzDepartments){
                    wdzDepartmentsService.insertWdzDepartments(wdzDepartments1);
                }
            }
        }
    }
    /**
     * 查询万店掌门店列表列表
     */
    @PreAuthorize("@ss.hasPermi('energize:departments:list')")
    @GetMapping("/list")
    public TableDataInfo list(WdzDepartments wdzDepartments)
    {
        startPage();
        List<WdzDepartments> list = wdzDepartmentsService.selectWdzDepartmentsList(wdzDepartments);
        return getDataTable(list);
    }

    /**
     * 导出万店掌门店列表列表
     */
    @PreAuthorize("@ss.hasPermi('energize:departments:export')")
    @Log(title = "万店掌门店列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WdzDepartments wdzDepartments)
    {
        List<WdzDepartments> list = wdzDepartmentsService.selectWdzDepartmentsList(wdzDepartments);
        ExcelUtil<WdzDepartments> util = new ExcelUtil<WdzDepartments>(WdzDepartments.class);
        util.exportExcel(response, list, "万店掌门店列表数据");
    }

    /**
     * 获取万店掌门店列表详细信息
     */
    @PreAuthorize("@ss.hasPermi('energize:departments:query')")
    @GetMapping(value = "/{autoId}")
    public AjaxResult getInfo(@PathVariable("autoId") Long autoId)
    {
        return success(wdzDepartmentsService.selectWdzDepartmentsByAutoId(autoId));
    }

    /**
     * 新增万店掌门店列表
     */
    @PreAuthorize("@ss.hasPermi('energize:departments:add')")
    @Log(title = "万店掌门店列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WdzDepartments wdzDepartments)
    {
        return toAjax(wdzDepartmentsService.insertWdzDepartments(wdzDepartments));
    }

    /**
     * 修改万店掌门店列表
     */
    @PreAuthorize("@ss.hasPermi('energize:departments:edit')")
    @Log(title = "万店掌门店列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WdzDepartments wdzDepartments)
    {
        return toAjax(wdzDepartmentsService.updateWdzDepartments(wdzDepartments));
    }

    /**
     * 删除万店掌门店列表
     */
    @PreAuthorize("@ss.hasPermi('energize:departments:remove')")
    @Log(title = "万店掌门店列表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{autoIds}")
    public AjaxResult remove(@PathVariable Long[] autoIds)
    {
        return toAjax(wdzDepartmentsService.deleteWdzDepartmentsByAutoIds(autoIds));
    }
}
